package com.maxlen.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.common.constant.WareConstant;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.ware.dao.PurchaseDetailDao;
import com.maxlen.ware.entity.PurchaseDetailEntity;
import com.maxlen.ware.entity.vo.PurchaseDetailPageParamsVO;
import com.maxlen.ware.service.PurchaseDetailService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(PurchaseDetailPageParamsVO params) {
        LambdaQueryWrapper<PurchaseDetailEntity> wrapper = new LambdaQueryWrapper<>();

        String key = params.getKey();
        if(StringUtils.hasText(key)){
            wrapper.like(PurchaseDetailEntity::getSkuNum, key);
        }

        String wareId = params.getWareId();
        if(StringUtils.hasText(wareId)){
            wrapper.eq(PurchaseDetailEntity::getWareId, wareId);
        }

        String status = params.getStatus();
        if(StringUtils.hasText(status)){
            wrapper.eq(PurchaseDetailEntity::getStatus, status);
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Page<>(params.getPage(), params.getLimit()),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据采购单 id 修改采购需求的状态为 正在采购
     * @param ids
     */
    @Override
    public void updateStatusByPurchaseIds(List<Long> ids) {
        PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
        purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailEnum.BUYING.getCode());
        baseMapper.update(purchaseDetailEntity,
                new LambdaQueryWrapper<PurchaseDetailEntity>().in(PurchaseDetailEntity::getPurchaseId, ids));
    }

    @Override
    public List<PurchaseDetailEntity> getListByIds(List<Long> purchaseDetailIds) {
        return baseMapper.selectBatchIds(purchaseDetailIds);
    }

}
